package projectmanagement.managementtool.ManagementToolServices;

import projectmanagement.managementtool.IterationServices.*;
import projectmanagement.managementtool.ProjectServices.*;
import projectmanagement.managementtool.UserServices.*;
import projectmanagement.managementtool.TaskServices.*;
import projectmanagement.managementtool.MeetingServices.*;
import projectmanagement.models.*;

import java.util.Calendar;
import java.util.Collection;
import java.util.List;

public class JiraManagementTool implements ManagementTool {

    private ProjectService projectService = new ProjectServiceImpl();
    private IterationService iterationService = new IterationServiceImpl();
    private UserService userService = new UserServiceImpl();
    private TaskService taskService = new TaskServiceImpl();
    private MeetingService meetingService = new MeetingServiceImp();


    public Meeting createMeeting(int projectId, String description, Calendar meetingStartTime,
                                 Calendar meetingDuration) {
        Project project = projectService.getProjectById(projectId);
        if(project == null)
            return null;
        else
            return meetingService.createMeeting(description,meetingStartTime,meetingDuration,project);
    }

    public Meeting getMeetingById(int projectId, int meetingId){
        Project project = projectService.getProjectById(projectId);
        if(project == null)
            return null;
        else
            return meetingService.getMeetingById(project,meetingId);
    }

    /**
     * @deprecated
     */
    public Iteration createIteration(int projectId, Calendar startDate, Calendar finishDate, String description) {
        Project project = projectService.getProjectById(projectId);
        return (project == null) ? null : iterationService.createIteration(project, startDate, finishDate, description);
    }

    public int scheduleIterations(int projectId, int duration) {
        if(duration == 0)
            return 0;
        Project project = projectService.getProjectById(projectId);
        return (project == null) ? -1 : iterationService.scheduleIterations(project, duration);
    }

    public Iteration getIterationById(int projectId, int iterationId) {
        Project project = projectService.getProjectById(projectId);
        return (project == null) ? null : iterationService.getIterationById(project, iterationId);
    }

    public Collection<Iteration> getListOfIterations(int projectId) {
        Project project = projectService.getProjectById(projectId);
        return (project == null) ? null : iterationService.getListOfIterations(project);
    }

    @Override
    public boolean addTask(int projectId, Task task) {
        return projectService.addTask(projectId,task);
    }

    public List<Task> getListOfTasks(int projectId){
        Project project = projectService.getProjectById(projectId);
        if(project == null)
            return null;
        else
            return project.getTaskList();
    }

    public Task getTaskById(int projectId,int taskId){
        Project p = projectService.getProjectById(projectId);
        if(p==null)
            return null;
        else
            return taskService.getTaskById(p,taskId);
    }

    public User getUserById(int userId) {
        return userService.getUserById(userId);
    }

   public void assignTaskToIteration(Project p,int taskId, int iterationId) {
         Iteration i = iterationService.getIterationById(p,iterationId);
         Task t = taskService.getTaskById(p,taskId);
         i.addTask(t);

    }

    public Project createProject(String projectName, Calendar projectStartDate, Calendar projectFinishDate, String projectDescription) {
        return projectService.createProject(projectName,projectStartDate,projectFinishDate,projectDescription);
    }

    public void updateProject(Project project) {
		projectService.updateProject(project);
	}

    public void deleteProject(Project project) {
    	projectService.deleteProject(project);
    }

    public Collection<Project> getProjectsList(){
        return projectService.showListOfProjects();
    }

    public User createUser(String name, int age, String position, String technologies) {
        return userService.createUser(name, age, position, technologies);
    }

    public Collection<User> getUsersList() {
        return userService.getUsersList();
    }

    public Project getProjectById(int projectId) {
        return projectService.getProjectById(projectId);
    }

    public Task createTask(int estimation, String description) {
        return taskService.createTask(estimation,description);
    }

	public boolean assignUserToTask(int projectId, int taskId, User u) {
		Project p = projectService.getProjectById(projectId);
		Task t = taskService.getTaskById(p, taskId);
		if((p==null)||(t==null))
			return false;
		t.setUser(u);
		return true;
	}

    public void assignTaskToUser(int taskId, int projectId, int userId) {
        Project project = projectService.getProjectById(projectId);
        Task task = taskService.getTaskById(project, taskId);
        if(task != null)
            userService.assignTask(userId, task);
    }

    public void assignUserToProject(int userId, int projectId) {
        userService.assignProject(userId, projectService.getProjectById(projectId));
    }

    public boolean isIterationIdValid(int projectId, int iterationId) {
        return getIterationById(projectId,iterationId) != null;
    }

    public boolean isTaskIdValid(int projectId,int taskId) {
        return getTaskById(projectId,taskId) != null;
    }

    public int getIdByProjectName(String name) {
        return projectService.getIdByProjectName(name);
    }

    public User getUserByName(String name) {
        return userService.getUserByName(name);
    }

    public boolean isProjectNameValid(String name) {
        return projectService.isProjectNameValid(name);
    }

    public Project getProjectByName(String name) {
        return projectService.getProjectByName(name);
    }
}